package com.ruoyi.project.module.wtc.controller;

import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.project.domain.YhAppointment;
import com.ruoyi.project.tool.HttpUtils;
import com.ruoyi.project.tool.WeChatAccessTokenUtils;
import com.ruoyi.project.tool.WeChatUtils;

import java.text.SimpleDateFormat;
import java.util.Locale;

public class WtcWeChatMassage {

    // 修改这里：方法参数可以更加内容进行修改，userid是必备的
    //public static void sendWeChatMassage(String userId,String username,String result) throws JSONException {
    public static void sendWeChatMassage(YhAppointment yhAppointment) throws JSONException {

        //System.out.println(userId);
        //System.out.println(username);
        //System.out.println(result);
        //时间标准化
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        String timeS = simpleDateFormat.format(yhAppointment.getAppointmentStart());
        String timeE = simpleDateFormat.format(yhAppointment.getAppointmentEnd());

        //基础企业信息和secret
        String corpid = "ww3c8d31017de5aa90";
        String corpsecret = "RYETl8kUJI8Nec1aWAGh9Q5wnRCC42UzaCuCq4aN5EQ";//设备预约系统，已经修改好

        // 获取access_token json字符串
        String accessToken = WeChatAccessTokenUtils.getAccessToken(corpid, corpsecret);
        // 发送应用消息
        String userid_url = WeChatUtils.QY_WEIXIN_MESSAGE_PUSH
                .replace("ACCESS_TOKEN", accessToken);

        JSONObject obj1 = new JSONObject();

        // 修改这里：可以根据userid查询用户其他信息，例如用姓名等 待完善

        // 修改这里：消息内容自己拼接

        // 接口文档地址：https://developer.work.weixin.qq.com/document/path/90236

        if (yhAppointment.getAuditStatus().equals("1")) {
            obj1.put("content", "【设备预约系统消息】\n" +
                    yhAppointment.getName() + " (" + yhAppointment.getStudentId() + ") " + "你好，你预约的设备已通过审核！\n\n" +
                    "设备编号：" + yhAppointment.getInstrumentId() + "\n" +
                    "预约设备：" + yhAppointment.getInstrumentName() + "\n" +
                    "存放位置：" + yhAppointment.getStorageLocation() + "\n" +
                    "预约时间：" + timeS + "至" + timeE + "\n\n" +
                    "请按时前往使用。<a href=\"http://spsbyy.bhu.edu.cn/prod-api/system/qywx/getCode\">详细信息查看</a>。");
        } else {
            obj1.put("content", "【设备预约系统消息】\n" +
                    yhAppointment.getName() + " (" + yhAppointment.getStudentId() + ") " + "你好，你预约的设备未能通过审核！\n\n" +
                    "设备编号：" + yhAppointment.getInstrumentId() + "\n" +
                    "预约设备：" + yhAppointment.getInstrumentName() + "\n" +
                    "存放位置：" + yhAppointment.getStorageLocation() + "\n" +
                    "预约时间：" + timeS + "至" + timeE + "\n\n" +
                    "<a href=\"http://spsbyy.bhu.edu.cn/prod-api/system/qywx/getCode\">详细信息查看</a>。");
        }

        JSONObject obj = new JSONObject();

        obj.put("touser", yhAppointment.getStudentId());
        obj.put("msgtype", "text");
        obj.put("agentid", "1000185");//设备预约系统，已经修改好
        obj.put("text", obj1);

        System.out.println(obj.toString());
        // 执行消息推送
        HttpUtils.sendPost(userid_url, obj.toString());

    }
}
